package com.ruoyi.project.jiaqin.workshop.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.project.jiaqin.company.domain.Company;
import com.ruoyi.project.jiaqin.company.service.ICompanyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.jiaqin.workshop.domain.CompanyWorkshop;
import com.ruoyi.project.jiaqin.workshop.service.ICompanyWorkshopService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 企业车间Controller
 *
 * @author admin
 * @date 2021-01-08
 */
@Controller
@RequestMapping("/jiaqin/workshop")
public class CompanyWorkshopController extends BaseController
{
    private String prefix = "jiaqin/workshop";

    @Autowired
    private ICompanyWorkshopService companyWorkshopService;
    @Autowired
    private ICompanyService iCompanyService;

    @RequiresPermissions("jiaqin:workshop:view")
    @GetMapping()
    public String workshop()
    {
        return prefix + "/workshop";
    }

    /**
     * 查询企业车间列表
     */
    @RequiresPermissions("jiaqin:workshop:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CompanyWorkshop companyWorkshop)
    {
        Company company = iCompanyService.selectCompanyBySysUserId(getSysUser().getUserId());
        if(company != null ){
            companyWorkshop.setCompanyId(company.getId());
            startPage();
            List<CompanyWorkshop> list = companyWorkshopService.selectCompanyWorkshopList(companyWorkshop);
            return getDataTable(list);
        }
        if(getSysUser().isAdmin()){
            startPage();
            List<CompanyWorkshop> list = companyWorkshopService.selectCompanyWorkshopList(companyWorkshop);
            return getDataTable(list);
        }

        return new TableDataInfo();

    }

    /**
     * 导出企业车间列表
     */
    @RequiresPermissions("jiaqin:workshop:export")
    @Log(title = "企业车间", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CompanyWorkshop companyWorkshop)
    {
        List<CompanyWorkshop> list = companyWorkshopService.selectCompanyWorkshopList(companyWorkshop);
        ExcelUtil<CompanyWorkshop> util = new ExcelUtil<CompanyWorkshop>(CompanyWorkshop.class);
        return util.exportExcel(list, "workshop");
    }

    /**
     * 新增企业车间
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        Company company = iCompanyService.selectCompanyBySysUserId(getSysUser().getUserId());
        if(company!=null){
            mmap.put("company",company);
            return prefix + "/addme";
        }

        return prefix + "/add";
    }

    /**
     * 新增保存企业车间
     */
    @RequiresPermissions("jiaqin:workshop:add")
    @Log(title = "企业车间", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CompanyWorkshop companyWorkshop)
    {
        companyWorkshop.setCreateAt(new Date());
        return toAjax(companyWorkshopService.insertCompanyWorkshop(companyWorkshop));
    }

    /**
     * 修改企业车间
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        CompanyWorkshop companyWorkshop = companyWorkshopService.selectCompanyWorkshopById(id);
        mmap.put("companyWorkshop", companyWorkshop);
        return prefix + "/edit";
    }

    /**
     * 修改保存企业车间
     */
    @RequiresPermissions("jiaqin:workshop:edit")
    @Log(title = "企业车间", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CompanyWorkshop companyWorkshop)
    {
        return toAjax(companyWorkshopService.updateCompanyWorkshop(companyWorkshop));
    }

    /**
     * 删除企业车间
     */
    @RequiresPermissions("jiaqin:workshop:remove")
    @Log(title = "企业车间", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(companyWorkshopService.deleteCompanyWorkshopByIds(ids));
    }
}
